package com.leyou.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.RegexUtils;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.service.UserService;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final String USER_SMS_SEND = "user:sms:phone";

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public UserDTO QueryInfo(String username, String password) {
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new LyException(400,"用户名或密码不能为空");
        }
        User user = query().eq("username", username).one();
        if(user==null || !bCryptPasswordEncoder.matches(password, user.getPassword())){
            throw new LyException(400,"用户名或密码错误");
        }
        return new UserDTO(user);
    }

    @Override
    public void registerUser(User user,String code) {
        String redisCode = redisTemplate.opsForValue().get(USER_SMS_SEND + user.getPhone());
        if(!code.equals(redisCode)){
            throw new LyException(400,"验证码错误");
        }
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        save(user);
    }

    @Override
    public void sendSMS(String phone) {
        if(!RegexUtils.isPhone(phone)){
            throw new LyException(400,"手机号格式错误");
        }

        Map<String,String> map = new HashMap<>();
        map.put("phone",phone);

        String code = RandomStringUtils.randomNumeric(6);
        map.put("code",code);

        this.rabbitTemplate.convertAndSend("smsJHJ","sms",map);
        this.redisTemplate.opsForValue().set(USER_SMS_SEND+phone,code,300, TimeUnit.SECONDS);
    }

    @Override
    public Boolean isExists(String data, Integer type) {
        if(StringUtils.isEmpty(data)){
            throw new LyException(400,"请求参数为空");
        }

        if(2==type && !RegexUtils.isPhone(data)){
            throw new LyException(400,"请求参数类型不匹配");
        }

        String dateType = null;
        switch (type) {
            case 1:
                dateType = "username";
                break;
            case 2:
                dateType = "phone";
                break;
            default:
                throw new LyException(400,"请求参数类型错误");
        }

        Integer count = query().eq(dateType, data).count();

        return count==1;
    }
}
